<?php

namespace app\company\controller;

use app\company\model\Warehouse;
use app\company\validate\WarehouseAreaValidate;
use app\service\traits\Crud;
use app\company\model\WarehouseArea as WarehouseAreaModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Response;

class WarehouseArea extends Base
{
    private WarehouseAreaModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new WarehouseAreaModel();
        $this->validateClass = WarehouseAreaValidate::class;
        $this->withSearch    = 'id,name,warehouse_id';
    }


    /**
     * 筛选框
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function search(): Response
    {
        $param = request()->param();
        $sql   = $this->model;
        if ($this->isGs) $sql = $sql->Gs();
        $sql           = $sql->withSearch($this->withSearch, $param);

        $warehouse_ids = Warehouse::gs()->column('id');
        $sql           = $sql->where('warehouse_id', 'in', $warehouse_ids);

        $count         = $sql->count();
        if (request()->param('page', 0)) {
            $sql = $sql->page($param['page'], $param['limit']);
        }
        $field = 'id';
        if (request()->param('field')) $field .= ',' . request()->param('field');
        $list = $sql->field($field)->order($this->orderSort)->select();
        return $this->success('获取成功', compact('list', 'count'));
    }
}